package com.livery.login.validators;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.livery.app.main.HibernateUtil;
import com.livery.data.UserLoginData;
import com.livery.login.SessionContext;


public class LoginValidator implements Validator {

	private String login;
	private String passwd;
	public LoginValidator(String login,String passwd) {
		this.login = login;
		this.passwd = passwd;
	} 
	@Override
	public boolean validate() {
		Session session =  HibernateUtil.getSessionFactory().openSession();
		UserLoginData info = (UserLoginData) session.createCriteria(UserLoginData.class).add(Restrictions.eq("passwd", passwd)).add(Restrictions.eq("login", login)).list().get(0);
		if(info != null){
			int kind = Integer.parseInt(info.getKind().trim());
			switch (kind) {
			case 0: SessionContext.getInstance().setWorker(info.getWorker(), SessionContext.PERMISSION.WORKER);break;
			case 1:	SessionContext.getInstance().setWorker(info.getWorker(), SessionContext.PERMISSION.SUPERVISOR);break;
			case 2:	SessionContext.getInstance().setWorker(info.getWorker(), SessionContext.PERMISSION.ADMIN);break;
			}
			
			return true;
		}else{
			return false;
		}
		
		
	}

}
